<?php defined('SYSPATH') or die('No direct script access.') ;
/**
 * KindEditor 编辑器封装类
 *
 * @see     http://www.kindsoft.net/doc.php
 * @author  Yellow.Chow <aultoale@gmail.com>
 * @version $Id: kindeditor.php 300 2012-07-09 08:55:28Z yellow.chow $
 * @license http://www.gnu.org/licenses/gpl-3.0.html    GPL 3
 */
class Editor_KindEditor extends Editor
{

    /**
     * JS 实例名
     *
     * @var string
     */
    protected $_instance = 'KEditor';

    /**
     * 风格样式
     *
     * @var string
     */
    protected $_style = 'default';

    /**
     * 样式选项
     *
     * @var array
     */
    protected $_styles = array(
        'default' => array(
            'source', '|', 'justifyleft', 'justifycenter', 'justifyright', 'justifyfull', '|',
            'insertorderedlist', 'insertunorderedlist', 'indent', 'outdent', '|', 'subscript',
            'superscript', '|', 'formatblock', 'fontname', 'fontsize', '|', 'forecolor',
            'hilitecolor', '|', 'bold', 'italic', 'underline', 'strikethrough', 'removeformat',
            '|', 'image', 'flash', 'media', 'insertfile', 'table', 'anchor', 'link', 'unlink'
        ),
        'simple' => array(
            'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', '|', 'bold', 'italic',
            'underline', 'removeformat', '|', 'justifyleft', 'justifycenter', 'justifyright',
            'insertorderedlist', 'insertunorderedlist', '|', 'emoticons', 'image', 'link',
        ),
        'full' => array(
            'source', 'preview', 'fullscreen', 'print', 'template', '|', 'undo', 'redo', '|',
            'cut', 'copy', 'paste', 'plainpaste', 'wordpaste', '|', 'justifyleft', 'justifycenter',
            'justifyright', 'justifyfull', '|', 'insertorderedlist', 'insertunorderedlist',
            'indent', 'outdent', 'subscript', 'superscript', '|', 'clearhtml', 'quickformat',
            'selectall', '/', 'formatblock', 'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor',
            '|', 'bold', 'italic', 'underline', 'strikethrough', 'lineheight', 'removeformat',
            '|', 'image', 'flash', 'media', 'insertfile', 'table', 'hr', 'emoticons', 'map',
            'code', 'pagebreak', 'anchor', 'link', 'unlink', '|', 'about'
        ),
        'comment' => array(
            'forecolor', 'hilitecolor', 'bold', 'italic', 'underline', 'emoticons', 'link',
        ),
    );

    /**
     * 输出 HTML 代码
     *
     * @return string
     */
    public function render()
    {
        $config = $this->_config;

        // 上传参数
        $config['uploadJson']       = URL::site($config['uploadJson']);
        $config['fileManagerJson']  = URL::site($config['fileManagerJson']);
        $config['allowFileManager'] = $this->_upload;
        $config['allowImageUpload'] = $this->_upload;
        $config['allowFlashUpload'] = $this->_upload;
        $config['allowMediaUpload'] = $this->_upload;
        $config['allowFileUpload']  = $this->_upload;

        // 尺寸
        $config['width'] = $this->_width;
        $config['height'] = $this->_height;

        if ( ! isset($this->_styles[$this->_style]))
        {
            throw new Editor_Exception('不支持的样式风格 ":style"', array(':style' => $this->_style));
        }

        // 工具栏/样式
        $config['items'] = $this->_styles[$this->_style];

        // 转换选项参数为 JSON 格式
        $json = JSON::encode($config, FALSE);

        // 加载 JS
        $html  = Editor::script('scripts/kindeditor/kindeditor-min.js');
        $html .= Editor::script('scripts/kindeditor/lang/zh_CN.js');

        $instance = isset($config['instance']) ? $config['instance'] : $this->_instance;

        // 绘制脚本
        $html .= <<<EOF
<textarea name="{$this->_name}" id="{$this->_name}" style="display:none">{$this->_value}</textarea>
<script type="text/javascript">
var {$instance};
KindEditor.ready(function(K) {
    {$instance} = K.create('textarea[name={$this->_name}]', {$json});
});
</script>
EOF;

        return $html;
    }

}